﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using Microsoft.Web.Helpers;
using MayhemServer.Models;

namespace MayhemServer.Controllers
{
	public class ContestController : Controller
	{
		//
		// GET: /Contest/

		public ActionResult Index()
		{
			return View();
		}

		[HttpGet]
		public ActionResult Enter()
		{
			ViewBag.Entry = new mayhem_Contest();
			return View();
		}

		[HttpPost]
		public ActionResult Enter([Bind(Exclude="Id")] mayhem_Contest entry)
		{
			ViewBag.Entry = entry;

			if (string.IsNullOrWhiteSpace(entry.FName))
				ViewData.ModelState.AddModelError("FName", "First Name must be set!");
			if (string.IsNullOrWhiteSpace(entry.LName))
				ViewData.ModelState.AddModelError("LName", "Last Name must be set!");
			if (string.IsNullOrWhiteSpace(entry.Email) || !Regex.IsMatch(entry.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
				ViewData.ModelState.AddModelError("Email", "Invalid email address!");
			if (string.IsNullOrWhiteSpace(entry.AddOn))
				ViewData.ModelState.AddModelError("AddOn", "Addon Name must be set!");
			if (string.IsNullOrWhiteSpace(entry.Youtube) || !entry.Youtube.Contains("youtube.com"))
				ViewData.ModelState.AddModelError("Youtube", "Youtube link must be set and valid!");
			if (string.IsNullOrWhiteSpace(entry.Source))
				ViewData.ModelState.AddModelError("Source", "Source repository link must be valid!");
			if (!ReCaptcha.Validate("6Le_Vc4SAAAAAOFK5MAkJluyz_UzBKokl8L3zMY9"))
			{
				ViewData.ModelState.AddModelError("recaptcha", "Invalid Recaptcha entered.");
			}

			if (ViewData.ModelState.IsValid)
			{
				mayhemserverEntities _db = new mayhemserverEntities();
				_db.AddTomayhem_Contest(entry);
				_db.SaveChanges(); 

				return RedirectToRoute("Success");
			}


			return View();
		}

		public ActionResult Success()
		{
			return View();
		}
	}
}
